#include "ItemBlockStateInBlock.h"
#include "EffectManager.h"


void ItemBlockStateInBlock::_initSprite()
{
	_sprites = new Sprites(_data->getRect());
	_sprites->addSprite(1, _ID_ITEM_BLOCK, 10);
	_sprites->setState(1);
}

ItemBlockStateInBlock::ItemBlockStateInBlock(ItemData* data)
{
	_data = data;
	_initSprite();
	_life = 3;
	_delaySprite = 4;
	_counter = 0;
}

void ItemBlockStateInBlock::updateData(int action, int action_data)
{
	if (action == ACTIONS_IS_HURT) {
		_life -= action_data;
		if (_life <= 0 ) {
			EffectManager::getInstatnse()->createEffect(EffectType::TYPE_Object_Fire, _data->getRect()->X, _data->getRect()->Y);
			_data->setState(StateFired);
		}
	}
}

void ItemBlockStateInBlock::draw(long time)
{
	_sprites->draw();
}

void ItemBlockStateInBlock::udpate(long time)
{
	// If block is closing, sprite will delay about a long time before opening.
	// but sprite only update after reach the delay time
	// so we must be count on delay time and make it more delay
	int currIndex = _sprites->currentSprite->currentIndex();
	if (currIndex == 0 || currIndex == 6) {
		// reset counter to make more delay with current index
		if (_counter <= 0) {
			_counter = 100;
		}
		_counter--;
		// check counter to start next sprite
		if (_counter <= 0) {
			_sprites->currentSprite->setIndex(1);
			_sprites->currentSprite->setStartEnd(0, 7);
		} 
	} else if (currIndex >= 2 && currIndex <= 4) {
		if (_counter <= 0) {
			_counter = 300;
		}
		_counter--;
		if (_counter > 0) {
			_sprites->currentSprite->setStartEnd(2,5);	
			_sprites->updateCurrent();
		} else {
			_sprites->currentSprite->setStartEnd(5, 7);
		}
	} else {
		_sprites->updateCurrent();
	}
	// set type make entity decide what current type is.
	_data->type = _ID_ITEM_BLOCK;
}
